//
//  SZMainViewController.m
//  03-仿网易新闻
//
//  Created by ItHeiMa on 2016/12/31.
//  Copyright © 2016年 itHeima. All rights reserved.
//

#import "SZMainViewController.h"

@interface SZMainViewController ()

@end

@implementation SZMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    
    //添加子控制器
    [self addChildViewControllers];
    
    //设置tabbar的tintColor
    self.tabBar.tintColor = [UIColor cz_colorWithHex:0xdf0000];
}

/**
 添加所有子控制器
 */
- (void) addChildViewControllers {
    //所有子控制器的参数
    NSArray *parameters = @[
                            @{@"clsName": @"SZHomeViewController", @"title": @"首页", @"icon": @"news"},
                            @{@"clsName": @"UIViewController", @"title": @"阅读", @"icon": @"reader"},
                            @{@"clsName": @"UIViewController", @"title": @"视频", @"icon": @"media"},
                            @{@"clsName": @"UIViewController", @"title": @"话题", @"icon": @"bar"},
                            @{@"clsName": @"UIViewController", @"title": @"我", @"icon": @"me"}
                            ];
    
    //子控制器数组
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSDictionary *dic in parameters) {
        UIViewController *controller = [self childViewControllerWithDic:dic];
        [array addObject:controller];
    }
    
    self.viewControllers = array;
}

/**
 创建一个子控制器

 @param dic 子控制器的参数字典
 @return 子控制器
 */
- (UIViewController *) childViewControllerWithDic:(NSDictionary *)dic {
    //用类名来创建一个控制器
    //好处：不需要导入头文件
    NSString *clsName = dic[@"clsName"];
    Class cls = NSClassFromString(clsName);
    UIViewController *controler = [cls new];
    
    //设title
    controler.title = dic[@"title"];
    
    //设tabbarItem的图标
    NSString *normalImageName = [NSString stringWithFormat:@"tabbar_icon_%@_normal", dic[@"icon"]];
    NSString *hlImageName = [NSString stringWithFormat:@"tabbar_icon_%@_highlight", dic[@"icon"]];

    [controler.tabBarItem setImage:[UIImage imageNamed:normalImageName]];
    [controler.tabBarItem setSelectedImage:[[UIImage imageNamed:hlImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    return [[UINavigationController alloc] initWithRootViewController:controler];
}

@end
